import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import java.io.*;
import java.applet.AppletContext.*;
import java.util.*;

import javax.swing.event.*;

import java.lang.reflect.*;

import javax.swing.border.*;
import javax.swing.plaf.basic.*;

import  java.awt.image.ImageConsumer ;
import java.awt.image.ImageObserver;
import   java.applet.AudioClip;

import javax.swing.filechooser.FileView;

import java.net.*;

import org.webswing.special.RedirectedDesktop;


public class ImageUploader extends JFrame
{
JTextField t1,t2,t3,t4,t5,t6;
JTextArea a1,a2,a3,a5,a4,a6,a7;
JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b10;
JLabel l1,l2,l3,l4,l5,l6,l7,l8,l9,l10,l11,l12,l13,l14;
JPanel jp1,jp2,jp3,jp4,jp5,jp6;
JFileChooser jFc;
 FileReader rd1;
 JTextField read1;
 FileWriter wr1;
Container cp;
private int _curId = -1;
private String crosstalkHost = "http://expressivecomputing.homelinux.org:56782";

ImageUploader(){
        super("Add Book To Library");
        getContentPane().setForeground(Color.RED);
        try{
        UIManager.setLookAndFeel ("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        }catch(Exception de){}
       
        sqlAPI dbx = new sqlAPI();
        _curId = dbx.getNextId("Books");
       
        setVisible(true);
        setSize(325,392);
        setLocation(150,130);
        cp=getContentPane();
        cp.setLayout(null);

        jp1=new JPanel(null,true);
        l1=new JLabel("Book Name");
        l1.setForeground(new Color(143, 188, 143));
        l1.setBackground(new Color(143, 188, 143));
        l1.setBounds(05,05,100,25);
        jp1.add(l1);

        t1=new JTextField();
        t1.setBounds(110,05,160,25);
        jp1.add(t1);

        l2=new JLabel("Book Author");
        l2.setBounds(05,40,100,25);
        jp1.add(l2);

        t2=new JTextField();
        t2.setBounds(110,40,160,25);
        jp1.add(t2);

        l3=new JLabel("Book Publication");
        l3.setForeground(new Color(143, 188, 143));
        l3.setBounds(05,75,100,25);
        jp1.add(l3);

        t3=new JTextField();
        t3.setBounds(110,75,160,25);
        jp1.add(t3);

        l4=new JLabel("Book Issue Date");
        l4.setBounds(05,110,100,25);
        jp1.add(l4);

        t4=new JTextField();
        t4.setBounds(110,110,160,25);
        jp1.add(t4);

        l5=new JLabel("Books Details");
        l5.setForeground(new Color(143, 188, 143));
        l5.setBounds(05,145,120,25);
        jp1.add(l5);

        a1=new JTextArea();
        a1.setBounds(110,145,160,70);
        jp1.add(a1);

        b1=new JButton("Save");
        b1.setBounds(10,319,90,25);
        jp1.add(b1);
       
        b2=new JButton("Reset");
        b2.setBounds(110,319,90,25);
        jp1.add(b2);
       
        b3=new JButton("Cancel");
        b3.setBounds(210,319,90,25);
        jp1.add(b3);

        Color r3=new Color(122,145,195);
        jp1.setBackground(new Color(0, 128, 128));
        jp1.setBounds(0,0,325,355);
        cp.add(jp1);
       
        JButton button = new JButton("Upload");
        button.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                        RedirectedDesktop rDesk = new RedirectedDesktop();
                        URI aURI = null;
                        try {
                                String theAddy = crosstalkHost + "/upload.php?cross=";
                                crossTalker temp = new crossTalker(_curId);
                                theAddy += temp.addImg();
                                aURI = new URI(theAddy);
                        } catch (Exception e) {
                                System.out.println("BadURI");
                        }
                        rDesk.browse(aURI);
                }
        });
        button.setBounds(110, 232, 90, 25);
        jp1.add(button);
       
       
        b1.addActionListener(new ActionListener()
        {
        public void actionPerformed(ActionEvent e)
        {
                //input validation
                String title, authorLastName, publisher, pubDate, desc;
                title = t1.getText();
                authorLastName = t2.getText();
                publisher = t3.getText();
                pubDate = t4.getText();
                desc = a1.getText();
                String inString = "";
                if(title.length() > 254) {
                        inString += "Title ";
                }
                if(authorLastName.length() > 254) {
                        inString += "Author Name ";
                }
                if(publisher.length() > 254) {
                        inString += "Publisher";
                }
                if(pubDate.length() > 245) {
                        inString += "Publish Date";
                }
               
                if(inString != "") {
                        JOptionPane.showMessageDialog(jp1, inString + "is greater than 254 characters, please shorten");
                }
               
                sqlAPI dbx = new sqlAPI();
                dbx.addBook(null, title, authorLastName, null, publisher, pubDate, desc, null);
                setVisible(false);
        }
        });

               
        b2.addActionListener(new ActionListener()
        {
        public void actionPerformed(ActionEvent e)
        {
        t1.setText("");
        t2.setText("");
        t3.setText("");
        t4.setText("");
        a1.setText("");
        }
        });

        b3.addActionListener(new ActionListener()
        {
        public void actionPerformed(ActionEvent e)
        {
           setVisible(false);
        }
        });

        }
}
